package com.nintendo.bubblechat.service.impl;

import com.nintendo.bubblechat.config.MinioConfig;
import com.nintendo.bubblechat.entity.dto.UserDTO;
import com.nintendo.bubblechat.entity.pojo.Users;
import com.nintendo.bubblechat.entity.vo.UsersVo;
import com.nintendo.bubblechat.mapper.UsersMapper;
import com.nintendo.bubblechat.netty.ChatMsg;
import com.nintendo.bubblechat.service.UsersService;
import com.nintendo.bubblechat.utils.*;
import org.n3r.idworker.Sid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import com.nintendo.bubblechat.service.UsersService;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UsersServiceImpl implements UsersService {
    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private MinioUtils minioUtils;
    @Autowired
    private QRCodeUtils qrCodeUtils;
    @Autowired
    private MinioConfig minioConfig;
    @Autowired
    private Sid sid;
    @Autowired
    private RedisOperator redisOperator;

    /**
     * 根据用户手机号注册新用户
     * @param phone
     * @return
     */
    @Override
    public UsersVo saveUser(String phone) {
        Users isExist = findUserByMobile(phone);
        UsersVo usersVo = new UsersVo();

        if(isExist!=null){
            UUID token = UUID.randomUUID();
            redisOperator.set(phone,token.toString());
            BeanUtils.copyProperties(isExist,usersVo);
        }else{
            String userId = sid.nextShort();
            //  为每个用户生成一个唯一的二维码
            String qrCodePath = "E://user"+userId+"qrcode.png";
            // muxin_qrcode:[username]
            qrCodeUtils.createQRCode(qrCodePath, "muxin_qrcode:"+phone);
            MultipartFile multipartFile = FileUtils.fileToMultipart(qrCodePath);
            String qrCodeUrl = "";
            try {
                qrCodeUrl = minioUtils.upload(multipartFile);
                qrCodeUrl = "http://" + minioConfig.getEndpoint() + ":" + minioConfig.getPort() + "/" + qrCodeUrl;
            } catch (Exception e) {
                e.printStackTrace();
            }
            Users users = new Users();
            users.setId(sid.nextShort());
            users.setQrCodeUrl(qrCodeUrl);
            try {
                users.setPassword(MD5Utils.getMD5Str("123456"));
            } catch (Exception e) {
                e.printStackTrace();
            }
            users.setBgImg("https://gips2.baidu.com/it/u=480684673,2208761962&fm=3042&app=3042&f=JPEG&wm=1,baiduai,0,0,13,9&wmo=0,0&w=480&h=640");
            users.setBubbleNum(phone);
            users.setFace("https://gips2.baidu.com/it/u=480684673,2208761962&fm=3042&app=3042&f=JPEG&wm=1,baiduai,0,0,13,9&wmo=0,0&w=480&h=640");
            users.setCreateTime(new Date());
            users.setUpdateTime(new Date());
            users.setDescription("");
            users.setSex(1);
            users.setCanShineNumBeUpdate(1);
            users.setBirthday(new Date());
            users.setNickname("[慕信:]"+phone);
            users.setMobile(phone);
            usersMapper.saveUser(users);

            BeanUtils.copyProperties(users,usersVo);
        }

        UUID token = UUID.randomUUID();
        redisOperator.set(phone,token.toString());
        usersVo.setToken(token.toString());
        return usersVo;
    }

    /**
     * 根据用户id修改用户
     * @param userDTO
     * @return
     */
    @Override
    public UsersVo updateUserInfo(UserDTO userDTO) {
        System.out.println("useDTo===>"+userDTO);
        usersMapper.updateUserInfoByUserId(userDTO);
        Users users = findUserByUserId(userDTO.getId());
        UsersVo usersVo = new UsersVo();
        UUID token = UUID.randomUUID();
        redisOperator.set(users.getMobile(),token.toString());
        usersVo.setToken(token.toString());
        BeanUtils.copyProperties(users,usersVo);
        return usersVo;
    }

    /**
     * 根据手机号查询用户
     * @param mobile
     * @return
     */
    @Override
    public UsersVo searchFriend(String mobile) {
        Users user = usersMapper.findUserByMobile(mobile);
        UsersVo usersVo = new UsersVo();
        if(user!=null){
            BeanUtils.copyProperties(user,usersVo);
        }
        return usersVo;
    }

    @Override
    public List<UsersVo> findUserByUserIds(List<String> requestUserIds) {
        if(requestUserIds!=null && requestUserIds.size()>0){
            List<UsersVo> usersVoList = usersMapper.findUserByUserIds(requestUserIds);
            return usersVoList;
        }
        return null;
    }

    @Override
    public UsersVo findFriendProfile(String friendUserId) {
        Users friend = usersMapper.findUserByUserId(friendUserId);
        UsersVo usersVo = new UsersVo();
        BeanUtils.copyProperties(friend,usersVo);
        return usersVo;
    }

    @Override
    public String saveMsg(ChatMsg chatMsg) {
        String msgId = sid.nextShort();
        com.nintendo.bubblechat.entity.pojo.ChatMsg msg = new com.nintendo.bubblechat.entity.pojo.ChatMsg();
        msg.setMsg(chatMsg.getMsg());
        msg.setId(msgId);
        msg.setSendUserId(chatMsg.getSenderId());
        msg.setAcceptUserId(chatMsg.getReceiverId());
        msg.setCreateTime(new Date());
        msg.setSignFlag(0);
        usersMapper.saveMsg(msg);
        return msgId;
    }

    @Override
    public void updateMsgSigned(List<String> msgIdList) {
        usersMapper.batchUpdateMsgSigned(msgIdList);
    }

    @Override
    public UsersVo findUserById(String senderId) {
        Users user = usersMapper.findUserByUserId(senderId);
        if(user!=null){
            UsersVo usersVo = new UsersVo();
            BeanUtils.copyProperties(user,usersVo);
            return usersVo;
        }
        return null;
    }

    private Users findUserByMobile(String phone){
        return usersMapper.findUserByMobile(phone);
    }
    private Users findUserByUserId(String id){
        return usersMapper.findUserByUserId(id);
    }
}
